﻿#ifndef TCP_SERVER_H
#define TCP_SERVER_H
#pragma execution_character_set("utf-8")
#include <QTcpServer>
#include <QObject>
#include <tcp_socket.h>
#include <QThread>
#include <QSharedPointer>
#include <QWeakPointer>
#include <handle_manage.h>
#include <common.h>
#include <m_socket.h>
class tcp_server:public QTcpServer
{
    Q_OBJECT
public:
    tcp_server(int Thread_Cout, int port);
    ~tcp_server();
    bool Start_Server();                                                                      //启动服务器
    void set_Handle(QWeakPointer<handle_manage> handlemanage);                                //设置handle
private:
    QMap  <tcp_socket*,QThread*> Socket_Thread;                                               //socket所在线程
    QMap  <QThread*,int>                        Thread_Socket_Cout;                           //线程socket数量
    QThread *Choice_Free_Thread();                                                            //选择空闲线程
    int Thread_Cout;                                                                          //线程数量
    int port;                                                                                 //监听端口
    QWeakPointer<handle_manage> handlemanage;                                                 //处理线程指针
    QTimer         TcpClock;                                                                  //定时器
protected:
    void incomingConnection(qintptr  socketDescriptor);                                       //当有新套接字连接时会调用此函数
private slots:
    void delete_tcpSocket();                                                                  //删除tcpsocket
signals:
    void disconnect_Allsocket();
    void delete_socket(tcp_socket *socket);

};

#endif // TCP_SERVER_H
